﻿Imports System.IO
Imports System.Text.RegularExpressions

''' <summary>
''' Used for performing validation tasks 
''' </summary>
''' <remarks></remarks>
''' <author>Share ChiWai</author>
Public Class ValidationHelper

    Public Shared Email_Rule As String = "^[0-9]*[a-zA-Z]([.\w]*[0-9a-zA-Z])*@([a-zA-Z][-\w\.]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$"
    Public Shared PostCode_Rule As String = "^[A-PR-UWYZ]((\d(\d|[A-HJKSTUW])?)|([A-HK-Y]\d(\d|[ABEHMNPRV-Y])?)) \d[ABD-HJLNP-UW-Z]{2}$"

    Public Shared IsNum_Rule As String = "^[0-9]{0,}$"



    Public Shared Function IsNum() As Boolean
        Dim result As Boolean
        Return result
    End Function

    ''' <summary>
    ''' Replace all items within ‘TextToReplace’ that occur within ‘Text’ with ‘ReplacesText’  
    ''' </summary>
    ''' <param name="Text">Text to look in</param>
    ''' <param name="TextToReplace">Array of items to look up</param>
    ''' <param name="ReplacedText">Text to replace with</param>
    ''' <returns>The output of the replaments</returns>
    ''' <remarks></remarks>
    Public Shared Function ReplaceText(ByVal Text As String, ByVal TextToReplace() As String, ByVal ReplacedText As String) As String
        Dim reg As New Regex("")

        For i As Int32 = 0 To TextToReplace.Length
            '  Regex.Replace(Text, TextToReplace(i), "")
            Text.Replace(TextToReplace(i), ReplacedText)
        Next
        Return Text
    End Function

    ''' <summary>
    ''' Gets file path from whole file path
    ''' e.g. //xxx/xxx/xxx/yyy.zzz returns //xxx/xxx/xxx 
    ''' </summary>
    ''' <param name="filePath">The file path</param>
    ''' <returns>Only the file path</returns>
    ''' <remarks></remarks>
    Public Function GetFilePathOnly(ByVal filePath As String) As String
        Dim i As Int32 = filePath.LastIndexOf("\")
        Return filePath.Substring(0, i)
    End Function

    ''' <summary>
    ''' Gets file name from path 
    ''' e.g. //xxx/xxx/xxx/yyy.zzz returns yyy.zzz
    ''' </summary>
    ''' <param name="filePath">the file path</param>
    ''' <returns>The file name</returns>
    ''' <remarks></remarks>
    Public Function GetFileName(ByVal filePath As String) As String
        Dim i As Int32 = filePath.LastIndexOf("\")
        Return filePath.Substring(i + 1)
    End Function

    ''' <summary>
    ''' Validates the customercode based on the length (must be 6 numbers)
    ''' </summary>
    ''' <param name="customerCode">Customer code</param>
    ''' <returns>True if the customer code is valid</returns>
    Public Shared Function ValidCustomerCode(ByVal customerCode As String) As Boolean
        Return Regex.IsMatch(customerCode, "^[0-9]{6}$")
    End Function

    ''' <summary>
    ''' Validates a number if its an integer or decimal or negative.
    ''' </summary>
    ''' <param name="number">A number that is being validated</param>
    ''' <returns>True if the number is valid</returns>
    Public Shared Function ValidateNumber(ByVal number As Double) As Boolean
        Return Regex.IsMatch(number.ToString(), "^(-|)(\d+\.\d+|0|\d+)$")
    End Function

    ''' <summary>
    ''' Validates an email address. Must be in the form xxx@xxx.xxx
    ''' </summary>
    ''' <param name="emailAddress">A number that is being validated</param>
    ''' <returns>True if the email is valid</returns>
    Public Shared Function ValidateEmailAddress(ByVal emailAddress As String) As Boolean
        Return Regex.IsMatch(emailAddress, Email_Rule)
    End Function

    ''' <summary>
    ''' Validates a postcode in the format AA9A 9AA etc
    ''' </summary>
    ''' <param name="postcode">A postcode that is being validated</param>
    ''' <returns>True if the number is valid</returns>
    Public Shared Function ValidatePostCode(ByVal postcode As String) As Boolean
        Return Regex.IsMatch(postcode, "^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z-[CIKMOV]]{2}$")
    End Function

    ''' <summary>
    ''' Validates a number if its an number
    ''' </summary>
    ''' <param name="number">A number that is being validated</param>
    ''' <returns>True if the number is valid</returns>
    Public Shared Function IsNum(ByVal number As String) As Boolean
        Return Regex.IsMatch(number, "^[0-9]{0,}$")
    End Function

End Class
